﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using NymphsLib.CommonUtilities.FileSystem;

namespace MovieBrowser.Model
{
    public partial class Movie
    {

        //Parsing Information
        public List<Genre> Genres { get; set; }
        public List<Country> Countries { get; set; }
        public List<Keyword> Keywords { get; set; }
        public List<Language> Languages { get; set; }
        public List<Person> PersonWriters { get; set; }
        public List<Person> PersonStars { get; set; }
        public List<Person> PersonDirectors { get; set; }
        public List<Actor> PersonActors { get; set; }
        //
        public bool Seen { get; set; }
        public bool Have { get; set; }
        public bool Watchlist { get; set; }
        public bool MovieChecked { get; set; }
        public bool Subtitle { get; set; }
        //
        public long Size { get; set; }
        //
        private static readonly List<string> MovieFiles = new List<string> { "avi", "mkv", "flv", "mp4" };
        private static readonly List<string> SubtitleFiles = new List<string> { "srt", "sub" };

        // Tree Purpose
        public List<Movie> Children { get; set; }
        public Movie Parent { get; set; }
        //

        public Movie()
        {
            CreationDate = DateTime.Today;
            Children = new List<Movie>();
        }

        public static Movie FromFolderName(string folderPath)
        {
            var folderName = "";
            var i = folderPath.LastIndexOf("\\");
            if (i > 0)
            {
                folderName = folderPath.Substring(i + 1);
            }

            var match = Regex.Match(folderName, @"(.+) \((\d+)\), \[([\d.]+)\]\s*(\[(tt\d+)\])?");
            if (match.Success)
            {
                return new Movie
                           {
                               Title = match.Groups[1].Value,
                               Year = int.Parse(match.Groups[2].Value),
                               Rating = double.Parse(match.Groups[3].Value),
                               ImdbId = match.Groups[5].Value,
                               FilePath = folderPath,
                               IsVirtual = false,
                               //Have = true,
                           };
            }
            return new Movie
                       {
                           Title = folderName,
                           FilePath = folderPath,
                           IsVirtual = false
                       };
        }

        public bool IsFilesystemFolder
        {
            get
            {
                return Directory.Exists(FilePath);
            }
        }
        public bool IsFolder { get; set; }
        public bool IsVirtual { get; set; }
        public bool IsValidMovie { get { return !string.IsNullOrEmpty(ImdbId); } }
        public string FilePath { get; set; }

        public string FolderName { get { return string.Format("{0} ({1}), [{2}] [{3}]", Title, Year, Rating, ImdbId).CleanFileName(); } }
        public int ImageIndex
        {
            get
            {
                if (IsValidMovie)
                    return 0; // Movie
                if (IsFilesystemFolder)
                    return 1; // Folder
                if (FilePath.Extension().ExistsIn(MovieFiles))
                    return 2; // Video
                return FilePath.Extension().ExistsIn(SubtitleFiles) ? 3 : 4;
            }
        }


        public static Movie Clone(Movie movie)
        {
            var m = new Movie();
            m.CloneFromMovie(movie);
            return m;
        }

        public Movie CloneFromMovie(Movie movie)
        {
            ImdbId = movie.ImdbId;
            Rating = movie.Rating;
            Title = movie.Title;
            Year = movie.Year;
            ImdbId = movie.ImdbId;
            FilePath = movie.FilePath;
            Runtime = movie.Runtime;
            MPAA = movie.MPAA;
            MPAAReason = movie.MPAAReason;
            Highlight = movie.Highlight;
            ReleaseDate = movie.ReleaseDate;
            //IsUpdated = movie.IsUpdated;
            Storyline = movie.Storyline;
            //
            Genres = movie.Genres;
            Countries = movie.Countries;
            Languages = movie.Languages;
            Keywords = movie.Keywords;
            //
            ImdbVoters = movie.ImdbVoters;
            ImdbCritics = movie.ImdbCritics;
            MetaScore = movie.MetaScore;
            return this;
        }
    }
}
